<?php
/**
 * Created by PhpStorm.
 * User: bill
 * Date: 2014/12/2
 * Time: 9:55
 */

class C后台管理员 extends  CBS
{

    public $id;
    public $user;
    public $公众号;
    public $权限;



    function __construct(){
        parent::__construct();

    }

    public function 获取所有推广人员及业绩($page,$参数,$stime,$etime) {
        $sql = sprintf("SELECT a.*,b.`name` FROM `用户推广表`  a LEFT JOIN  `用户`b ON( a.`用户id`= b.id) WHERE b.`公众号ID`= '%s'", G_SERV_ID);
        return pdo()->page($sql,$page,15,$参数);
    }

    public static function get获取全部消息记录($公众号id,$当前页码,$显示数量,$参数) {
        $sql = "select a.*,b.name from 消息发送记录 a left join 用户 b on a.接收消息用户openID=b.openID  where a.公众号ID=$公众号id order by a.错误代码 desc , a.id desc";
        $查询结果数组 = pdo()->page($sql,$当前页码,$显示数量,$参数);
        return $查询结果数组;
    }


    /**
     * @2015-07-14
     * @得到所有地推二维码
     */

    public static function get得到所有地推二维码($公众号id,$page,$pagelist,$parm) {
        $sql = 'select * from qrcode where note="未关联用户的二维码" and 公众号ID='.$公众号id;
        $查询结果数组 = pdo()->page($sql,$page,$pagelist,$parm);
        return $查询结果数组;
    }

    public function fd登陆检验($user,$pass){

        $用户信息=pdo()->selectColumns("后台管理员",array('pass','公众号openID','公众号ID','权限'),array("name"=>$user));
        if(trim($用户信息['pass'])==md5(trim($pass))) {
            $_SESSION['user'] = $user;
            $_SESSION['id'] = $用户信息['id'];
            $_SESSION['公众号openID'] = $用户信息['公众号openID'];
            $_SESSION['公众号ID'] = $用户信息['公众号ID'];
            $_SESSION['权限'] = $用户信息['权限'];
            return true;
        }else{
            return false;
        }
    }


    /**
     * @param array $参数  //$appid,$认证=null,$昵称=null
     * @param null $star
     * @param null $limit
     * @return array|bool
     */
    public function f显示公众号的用户(array $参数,$star=null,$limit=null){
        if(!$参数[0]) return false;
        $显示= new Do用户();
        if($参数[1]){
            $sql="select * ,t1.id as `id显示`,(select count(*) from 会员卡 WHERE 商户id=t1.id)AS `会员卡数` ,(select count(*) from 会员卡 WHERE 商户id=t1.id and `卡状态`= '未激活')AS 未激活  ,b.`二维码编号`  from `用户` as t1   LEFT JOIN `用户推广表` b ON( t1.id= b.`用户id`)   WHERE t1.`公众号ID`='".$参数[0]."' and  t1.认证 =".$参数[1]."  limit ".$star.",".$limit;

            //  $全部店铺 =pdo()->selectBOs($显示,array('公众号openID'=>$appid,'认证'=>$认证),'',"",array(),$star,$limit);
        }else if($参数[2]){
            $sql="select * , t1.id as `id显示`,(select count(*) from 会员卡 WHERE 商户id=t1.id)AS `会员卡数` ,(select count(*) from 会员卡 WHERE 商户id=t1.id and `卡状态`= '未激活')AS 未激活 ,b.`二维码编号`  from `用户` as t1   LEFT JOIN `用户推广表` b ON( t1.id= b.`用户id`)    WHERE t1. `公众号ID`='".$参数[0]."' and t1.name like '%".$参数[2]."%' or t1. `公众号ID`='".$参数[0]."' and t1.seller_name like '%".$参数[2]."%'   limit ".$star.",".$limit;
            // $全部店铺=pdo()->selectBOs($显示,array(),"`公众号openID`='".$appid."' and name like '%".$昵称."%'","",array(),$star,$limit);
        }else{
            $sql="select * ,t1.id as `id显示`,(select count(*) from 会员卡 WHERE 商户id=t1.id)AS `会员卡数` ,(select count(*) from 会员卡 WHERE 商户id=t1.id and `卡状态`= '未激活')AS 未激活,b.`二维码编号`  from `用户` as t1   LEFT JOIN `用户推广表` b ON( t1.id= b.`用户id`)   WHERE t1.`公众号ID`='".$参数[0]."' limit ".$star.",".$limit;

        }
        $全部店铺 =pdo()->selectAll($sql);
        foreach($全部店铺 as $key => $value){
            $全部店铺[$key]['客户数']=$全部店铺[$key]['会员卡数']-$全部店铺[$key]['未激活'];
        }

        return $全部店铺;
    }

    //
    /**
     * @param array $参数   $appid,$认证=null,$昵称=null
     * @return int
     */
    public function  f显示公众号的用户个数(array $参数){
        if(!$参数[0]) return false;
        $显示= new Do用户();
        if($参数[1]){
            $全部店铺 =pdo()->selectBOs($显示,array('公众号ID'=>$参数[0],'认证'=>$参数[1]));
        }else if($参数[2]){
            $全部店铺=pdo()->selectBOs($显示,array(),"`公众号ID`='".$参数[0]."' and name like '%".$参数[2]."%'");
        }else{
            $全部店铺 =pdo()->selectBOs($显示,array('公众号ID'=>$参数[0]));
        }
         if($全部店铺){
             return count($全部店铺);
         }else{
             return 0;
         }

    }

    /**
     * @param int $p
     * @param $查看总数方法
     * @param array $查看总数参数
     * @param $查看详情方法
     * @return array
     */
    public function f分页($p=1,$查看总数方法,array $查看总数参数,$查看详情方法){
        $总数= $this->$查看总数方法($查看总数参数);
        $每页总数=10;
        $总页数=ceil($总数/$每页总数);
        $当前页=1;
        if($p>1){
            $当前页=$p;
            $p上一页=$当前页-1;
        }
        $p下一页=$当前页+1;
        $star=($当前页-1)*$每页总数;
        $r显示 =$this->$查看详情方法($查看总数参数,$star,$每页总数);

            $str= "<div style='text-align:center'><span style='cursor:pointer; padding: 0 3px;'  onclick='f分页(1)'>首页 </span>";
            if(isset($p上一页)){$str.="<span  style='cursor:pointer; padding: 0 3px;'  onclick='f分页(".$p上一页.")'>上一页 </span> ";}
            $str.= "<span  style=' padding: 0 3px;'>".$当前页."</span>";
            if($当前页< ceil($总数/$每页总数)){$str.="<span  style='cursor:pointer; padding: 0 3px;' onclick='f分页(".$p下一页.")'>下一页 </span> ";}
            $str.= "<span  style='cursor:pointer; padding: 0 3px;' onclick='f分页(".ceil($总数/$每页总数).")'>尾页 </span><span  style=' padding: 0 3px;' >当前页".$当前页."/".$总页数."</span><span style=' padding: 0 3px;'>总条数:".$总数."</span></div>";
            $返回=array();
            $返回['page']=$str;
            $返回['content']=$r显示;
            return $返回;

    }
    private function f显示公众号详情(array $参数,$star=null,$limit=null){
        $显示=pdo()->selectAll("select * from `".$参数[0]."` where 公众号ID='".G_SERV_ID."' order by id desc limit ".$star.",".$limit);
       return $显示;

    }
    private function f显示公众号的个数(array $参数){
        $总数=pdo()->selectAll("select count(*) as num from `".$参数[0]."` where 公众号ID='".G_SERV_ID."'");
       return $总数[0]['num'];

    }
    public function  f截取字符串($str,$start,$len) {//$str是指字符串，$start指字符串的起始位置，$len指字符串长度
        $len=$len*3;
        $strlen=$start+$len; //用$strlen存储字符串的总长度
        $tmpstr='';
        for($i=0;$i<$strlen;$i++) {
            if(ord(substr($str,$i,1))>0xa0) { //如果字符串中出现汉字，也就是ASC码大于0xa0的。作出判断与英文字符不一样。
                $tmpstr.=mb_substr($str,$i,2,'utf-8');
                //$tmpstr.=substr($str,$i,2);
                $i++;
            }
            else
                $tmpstr.=mb_substr($str,$i,1,'utf-8');
        }
        return $tmpstr;
    }

    public function f显示公众号的用户汇总($公众号ID,$stime=null,$etime=null){
        $startdate = $stime ? $stime : date("Ymd");
        $enddate = $etime ? $etime : (string)($startdate+1);
        $ret = self::get查询汇总数据_公众号($公众号ID,$startdate,$enddate);
        return json_encode($ret);
    }
    public function f显示推广用户($二维码编号,$stime=null,$etime=null){
        $startdate = $stime ? $stime : date("Ymd");
        $enddate = $etime ? $etime : (string)($startdate+1);
        $ret = self::get查询推广用户($二维码编号,$startdate,$enddate);
        return json_encode($ret);
    }

    private function get查询推广用户($二维码编号, $startdate, $enddate){
        $sql="SELECT
'".$startdate."' as 开始时间, '".$enddate."' as 结束时间  , a.会员id,a.会员名称,
IFNULL(sum(case when a.消费类型 in (8,9) then 1 else 0 end),0) as 充值笔数,
IFNULL(sum(case when a.消费类型 in (8,9)   then 金额 else 0 end),0) as 充值金额,
IFNULL(sum(case when a.消费类型 in (1)   then 1 else 0 end),0) as 消费笔数,
IFNULL(sum(case when a.消费类型 in (1)   then 金额 else 0 end),0) as 消费金额,
IFNULL(sum(case when a.消费类型 in (5)  then 1 else 0 end),0) as 纠正笔数,
IFNULL(sum(case when a.消费类型 in (5)  then 金额 else 0 end),0) as 纠正金额,
IFNULL(sum(case when a.消费类型 in (6)  then 1 else 0 end),0) as 退款笔数,
IFNULL(sum(case when a.消费类型 in (6)  then 金额 else 0 end),0) as 退款金额,
IFNULL(sum(case when a.消费类型 in (7)  then 1 else 0 end),0) as 赠送笔数,
IFNULL(sum(case when a.消费类型 in (7)  then 金额 else 0 end),0) as 赠送金额,
IFNULL(sum(case when a.消费类型 in (4)  then 1 else 0 end),0) as 销卡笔数,
IFNULL(sum(case when a.消费类型 in (4)  then 金额 else 0 end),0) as 销卡金额,
SUM(a.`金额`) as 总金额

 FROM `消费记录` a LEFT JOIN `用户推广详细` b ON( a.`会员id`= b.uid)
WHERE b.二维码编号='".$二维码编号."' and
 a.`消费时间` >='".$startdate."' and a.`消费时间` <'".$enddate."' group by a. `会员id` ";

        $ret=pdo()->selects($sql);

        $rettotal=array();
        foreach ($ret as $key => $val) {
            if (!empty($val))
                $rettotal[]=$val ;
        }
        return$rettotal ;
    }

    private function get查询汇总数据_公众号($公众号ID, $startdate, $enddate)
    {
      //  $startdate=str_replace("-","",str_replace());
        $sql= "select
商户id,'".$startdate."' as 开始时间, '".$enddate."' as 结束时间  , 商户oid,商户名称,
'现金充值' as 项目名称,
IFNULL(sum(case when 消费类型 in (8,9) then 1 else 0 end),0) as 充值笔数,
IFNULL(sum(case when 消费类型 in (8,9)   then 金额 else 0 end),0) as 充值金额,
IFNULL(sum(case when 消费类型 in (1)   then 1 else 0 end),0) as 消费笔数,
IFNULL(sum(case when 消费类型 in (1)   then 金额 else 0 end),0) as 消费金额,
IFNULL(sum(case when 消费类型 in (5)  then 1 else 0 end),0) as 纠正笔数,
IFNULL(sum(case when 消费类型 in (5)  then 金额 else 0 end),0) as 纠正金额,
IFNULL(sum(case when 消费类型 in (6)  then 1 else 0 end),0) as 退款笔数,
IFNULL(sum(case when 消费类型 in (6)  then 金额 else 0 end),0) as 退款金额,
IFNULL(sum(case when 消费类型 in (7)  then 1 else 0 end),0) as 赠送笔数,
IFNULL(sum(case when 消费类型 in (7)  then 金额 else 0 end),0) as 赠送金额,
IFNULL(sum(case when 消费类型 in (4)  then 1 else 0 end),0) as 销卡笔数,
IFNULL(sum(case when 消费类型 in (4)  then 金额 else 0 end),0) as 销卡金额
from 消费记录 t1 where   `公众号ID`='".$公众号ID."'
and t1.`消费时间` >='".$startdate."' and t1.`消费时间` <'".$enddate."'     group by  `商户id`";

        $ret=pdo()->selects($sql);

        $rettotal=array();
        foreach ($ret as $key => $val) {
            if (!empty($val))
                $rettotal[]=$val ;
        }
        return$rettotal ;
    }



    private  function  fp判断数组key是否存在($字段,$数组){
        $data=array();
        if (array_key_exists($字段,$数组)) {//存在键key
           return $数组[$字段];
        }else{//不存在键key
            return $数组[$字段]=0;
        }
    }
    public function fs删除商户($id){
        $rid=$this->fj检验用户是否存在($id);

        if($rid){
            if($rid==1){
              $re=pdo()->update("用户",array("是否正常"=>2),array("id"=>$id));
                return $re;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }

    public function fh生成用户推广二维码($id){
        $rid=$this->fj检验用户是否存在($id);
            if($rid==1){
               $是否存在= $this->fh查看用户是否生成推广二维码($id);
                if($是否存在){
                    return 2;
                }
                $createcode= new createcode(1,G_SERV_ID);
                $二维码编号=$createcode->f创建二维码("ygtg",$id);
                if($二维码编号){
                    $do= new Do用户推广表();
                    $do->set_二维码编号($二维码编号);
                    $do->set_用户id($id);
                    $do->set_数量(0);
                    $re=$do->save();
                    return $re;
                }else{
                    return true;
                }


               // $re=pdo()->update("用户",array("是否正常"=>1),array("id"=>$id));
              //  return $re;
            }else{
                return true;
            }

    }
    private function fh查看用户是否生成推广二维码($id){
        $id= pdo()->selectColumns("用户推广表",array(id),array("用户id"=>$id));
        if($id)return true;
        else return false;
    }

    private function fj检验用户是否存在($id){

       $id= pdo()->selectColumns("用户",array("是否正常"),array("id"=>$id));
        if($id)return true;
        else return false;
    }

    public function fc查找所有店长(){
        $do  =  new Do用户();
        return pdo()->selectBOs($do,array('认证'=>'2','公众号ID'=>$_SESSION['公众号ID']));

    }

    public function fc查找用户信息($name){
        $条件 = "";
        if($name) $条件 .= " and a.`name` like '%".$name."%'  ";
        $sql="select a.id,a.`name`,b.`医院` from `用户` a  LEFT JOIN `医师详情` b  ON (a.id= b.`用户id`)  WHERE  a.`公众号ID` =".G_SERV_ID." ".$条件." order by a.id desc ";
        $用户信息 = pdo()->selectAll($sql);

        return $用户信息;
    }


    public function fc查看用户是否登录($时间限制){
        $用户 = new Do用户();
        $未使用公众号用户 = pdo()->selectBOs($用户,array(),"最后登录时间 < '".$时间限制."'");

        if(!$未使用公众号用户) return false;

        foreach($未使用公众号用户 as $key=>$value){
            $voCenter=C信息中心::get实列根据用户Oid($value->data['openID']);
           /// $parms=['type'=>'离线通知','用户id'=>$value->data['id'],'openID'=>$value->data['openID']];
            $infoUser="⭕离线通知:您即将离线,为保证收到消息,请随意点击菜单";
            $voCenter->ff发送文本消息_多公众号($infoUser,$value->data['openID'],$value->data['公众号ID']);

           // $ret=$voCenter->vc微直达->给用户发送保存在线状态($parms);
          //  $ret2=$voCenter->ff发送图文消息_多公众号($ret,$value->data['openID'],$value->data['公众号ID']);
        }

        Cx消息路由::ff处理返回堆栈();
    }


    public function fc查看课程即将开课($时间限制) {

        $sql= "SELECT * FROM 语音课程 WHERE `开始时间`  BETWEEN  '".date('Y-m-d H:i:s')."' and '".$时间限制."'   ";

        $课程=  pdo()->selectAll($sql);
        if(!$课程) return false;

        foreach($课程 as $key=>$value){

            $this->fs给报名课程的用户发送消息($value);

        }

        #Cx消息路由::ff处理返回堆栈();
    }

    public function f推送单个课程消息($课程id) {
        $sql = sprintf("SELECT * FROM 语音课程 WHERE id = '%s'", $课程id);
        $课程 = pdo()->selectOne($sql);
        if ($课程) {
            $this->fs给报名课程的用户发送消息($课程);
        }
    }

    public function fs给报名课程的用户发送消息($课程) {
        if ($课程['is_test']) {
            $用户信息 =  $this->fh获取测试用户();
        } else {
            $用户信息 =  $this->fh获取当前报名该课程的用户($课程['id']);
        }

        $url = Tools::url('course','course_detail', array('id' => $课程['id']));

        $voCenter = $this->fs实例化信息中心('');

        foreach ($用户信息 as $value) {
            $图文信息 = $voCenter->vc微直达->给用户发送语音课程信息("视频课程将要开始",'课程《'.$课程['title'].'》将要开始，请按时进入课堂!',$课程['id']);

            $m模板信息 = array(
                'id' => $课程['id'],
                '咨询模板ID' => $课程['course_notice_templateId'],
                'userName' => $value['name'],
                'courseName' => $课程['title'],
                'date' => $课程['开始时间'],
                'remark' => $课程['简介'],
            );

            $模板类型 = '开课通知';
            $结果 = '';
            $this->fs发送咨询课程_图文模板信息($voCenter, $图文信息, $value, $m模板信息, $结果, $模板类型, $url);

        }
        return true;
    }

    function f页面链接($文件名, $va参数) {
        $va参数['publicnumber'] = G_SERV_ID;
        $state=Tools::pack参数打包($文件名,$va参数);
        $url = G_SITE_BASE . '/business/index.php?state='.$state;
        return $url;
    }

    function f页面链接_new($m,$a, $parm) {
        $url=Tools::url($m,$a,$parm);

       // $va参数['publicnumber'] = G_SERV_ID;
       // $state=Tools::pack参数打包($文件名,$va参数);
       // $url = G_SITE_BASE . '/business/index.php?state='.$state;
        return $url;
    }

    function fh获取测试用户() {
        $sql = "SELECT b.`name`,b.openID,b.`公众号ID`,b.`公众号openID` FROM  `test_user` a LEFT JOIN `用户` b on(a.`openId` = b.openId)";
        return pdo()->selectAll($sql);
    }

    public function fh获取当前报名该课程的用户($课程id) {
        if (!empty($_POST['oid'])) {
            #发给单个测试人员
            $sql = sprintf("SELECT a.`课程id`,b.`name`,b.openID,b.`公众号ID`,b.`公众号openID` FROM  `语音课程报名` a LEFT JOIN `用户` b on(a.`用户id` = b.id) WHERE a.`课程id`= '%s' and b.openID = '%s' and a.`状态` = '报名' group by b.openID", $课程id, $_POST['oid']);
        } else {
            $sql = sprintf("SELECT a.`课程id`,b.`name`,b.openID,b.`公众号ID`,b.`公众号openID` FROM  `语音课程报名` a LEFT JOIN `用户` b on(a.`用户id` = b.id) WHERE a.`课程id`= '%s' and a.`状态` = '报名' group by b.openID", $课程id);
        }
        Logger::getInstance()->logInfo("获取当前报名该课程的用户sql:". $sql);
        $用户 = pdo()->selectAll($sql);
        return $用户;
    }


    public  function fc判断当前时间内专家有课($专家id,$开始时间,$结束时间,$课程id =0){
       $sql = "SELECT count(a.`id`) as `num`  FROM `语音课程报名` a LEFT JOIN `语音课程` b  ON a.`课程id`=b.id WHERE a.`用户id` ='".$专家id."'   AND   b.`状态` !='结束'  AND b.id != '".$课程id."'
 AND a.`状态`='报名' AND ( b.`开始时间` between  '".$开始时间."' AND '".$结束时间."' OR  b.`结束时间` between   '".$开始时间."' AND '".$结束时间."');";
        $时间冲突 = pdo()->selectAll($sql);
        return $时间冲突[0]['num'];
    }

    public function fc发送图文消息_所有用户($类别, $课程id, $用户oid =null, $结果=''){

        $voCenter = $this->fs实例化信息中心($用户oid);
        $url = Tools::url('course','course_detail', array('id' => $课程id));
        $课程详情 = pdo()->selectOne("SELECT a.*,b.`name` FROM `语音课程` a LEFT JOIN `用户` b on(a.`讲课专家id` = b.id) WHERE a.id='".$课程id."' ");
        $图文信息=$voCenter->vc微直达->给用户发送语音课程信息($类别,$课程详情['theme'],$课程id);
        $数据表 = '课程';
        $this->ff发送咨询信息过程($voCenter,$图文信息,$课程详情,$结果,$数据表,$url,$用户oid);
        return true;
    }

    //发送最美医生投票推送
    public function send_vote($vote_id,$user_oid =null,$结果='') {

        $voCenter = $this->fs实例化信息中心($user_oid);
        $url = Tools::url('vote', 'index',array('id'=>$vote_id));
        $doctor_deatail = pdo()->selectColumns("vote_beautiful_doctor",array('*'),array('id'=>$vote_id));
        $图文信息 = $voCenter->vc微直达->添加要图文信息1($doctor_deatail);

        $数据表 = '投票';
        $m模板信息 = $doctor_deatail;
        $this->ff发送咨询信息过程($voCenter,$图文信息,$m模板信息,$结果,$数据表,$url,$user_oid);
    }

    //判断咨询类型，普通咨询，课程回顾
    function is_exits_case($zixun_id){
        $do = new Do咨询详情();
        $do->set_咨询id($zixun_id);
        $re = $do->where($do->data)->rows();
        foreach($re as $k => $v) {
            if($v['news_type'] == 2){
                return '2';
            }
        }
        return '1';
    }

    public function ff发送咨询信息($咨询id,$用户oid =null,$结果=''){

        $voCenter = $this->fs实例化信息中心($用户oid);
        $news_type = $this->is_exits_case($咨询id);

        if($news_type == '1'){
            $url = Tools::url('iread', 'index');
        }else{
            $url = Tools::url('course', 'course_history');

        }

        $m模板信息 = pdo()->selectColumns("咨询信息",array('*'),array('id'=>$咨询id));
        $do= new Do咨询详情();
        $咨询详情=pdo()->selectBOs($do,array("咨询id"=>$咨询id),'',' order by `排序` ');

        $图文信息=$voCenter->vc微直达->添加要图文信息($咨询详情);
        $数据表 = '咨询';

        $this->ff发送咨询信息过程($voCenter,$图文信息,$m模板信息,$结果,$数据表,$url,$用户oid);
    }

    private function ff发送咨询信息过程(C信息中心 $voCenter,$图文信息,$m模板信息,$结果,$数据表,$url,$用户oid){

        #发给单个测试用户
        if($用户oid){
            $statement = pdo()->prepare_execute("select openID,id,公众号ID,公众号openID from 用户 where 公众号ID = ? AND  openID =? ORDER BY id",array(G_SERV_ID,$用户oid));

        #测试课程只发给测试人员
        } elseif ($数据表 == '课程' and $m模板信息['is_test'] == '1') {
            $statement = pdo()->prepare_execute("select u.openID,u.id,u.公众号ID,u.公众号openID from test_user t left join 用户 u on t.openID = u.openID where u.公众号ID = ? AND u.unsubscribe = 0 ORDER BY u.id",array(G_SERV_ID));

        #发给全部用户
        } else {
            $statement = pdo()->prepare_execute("select openID,id,公众号ID,公众号openID from 用户 where 公众号ID = ? AND unsubscribe = 0 ORDER BY id",array(G_SERV_ID));
        }

        if (false == $statement) {
            logger::getInstance()->logDebug2("选用户发图文消息出错");
            return false;
        }

        while(true) {
            $data = $statement->fetch(PDO::FETCH_ASSOC);

            if ($data == false){
                break; //无结果
            }
             $this->fs发送咨询课程_图文模板信息($voCenter,$图文信息,$data,$m模板信息,$结果,$数据表,$url);
        }
    }

    private function  fs发送咨询课程_图文模板信息(C信息中心 $voCenter,$图文信息,$data,$m模板信息,$结果,$数据表,$url){

        #判断是否成功发送过
        $do消息发送记录 = new Do消息发送记录();
        $do消息发送记录->set_接收消息用户openID($data['openID']);
        $do消息发送记录->set_信息id($数据表.$m模板信息['id']);
        $do消息发送记录->set_是否正确发送('是');
        $is_receive_msg = $do消息发送记录->where($do消息发送记录->data)->count();

        #判断是否是测试人员
        $do_test_user = new DoTest_user();
        $do_test_user->set_openId($data['openID']);
        $is_test_user = $do_test_user->where($do_test_user->data)->count();

        #非测试人员只能收一条消息
        if ($is_receive_msg and !$is_test_user) {
            return true;
        }

        $发送图文返回 = $voCenter->ff发送图文消息($图文信息,$data['openID']);
        $发送图文返回 = json_decode($发送图文返回);

        if($发送图文返回->errmsg != 'ok'){
            if($数据表=='课程') {
                $发送模板返回 = $this->fs发送课程提醒模板($data['openID'], $m模板信息, $voCenter, $url);
            } elseif ($数据表=='开课通知') {
                $发送模板返回 = $this->fs发送_课程开课通知_模板($data['openID'], $m模板信息, $voCenter, $url);
            } elseif($数据表=='投票'){
                $发送模板返回 = $this->fs发送审核提醒模板($data['openID'],$m模板信息,$voCenter,$url);
            } else{

                $发送模板返回 = $this->fs发送作业消息模板($data['openID'],$m模板信息,$voCenter,$url);
            }

            if($发送模板返回->errmsg == 'ok'){
                $this->fs发送信息记录($data['openID'],'作业消息模板','是',$发送模板返回->errcode,$m模板信息['标题'],$data['公众号ID'],$data['公众号openID'],$数据表.$m模板信息['id']);
            }else{
                $this->fs发送信息记录($data['openID'],'作业消息模板','否',$发送模板返回->errcode,$m模板信息['标题'] ,$data['公众号ID'],$data['公众号openID'],$数据表.$m模板信息['id']);
            }


        }else{
            $this->fs发送信息记录($data['openID'],'图文消息','是',$发送图文返回->errcode ,$m模板信息['标题'] ,$data['公众号ID'],$data['公众号openID'],$数据表.$m模板信息['id']);
        }

    }

    public function fc发送错误咨询_作业信息(C信息中心 $voCenter,$图文信息,$data,$m模板信息,$url,$数据表){

        $发送结果 =pdo()->selectColumns('消息发送记录',array('*'),array('接收消息用户openID'=>$data['openID'],'信息id'=>$数据表.$m模板信息['id']));


        if($发送结果['是否正确发送']=='是') return true;

        $发送图文返回 = $voCenter->ff发送图文消息($图文信息,$data['openID']);
        $发送图文返回 = json_decode($发送图文返回);


        if($发送图文返回->errmsg != 'ok'){

            if($数据表=='课程'){
                $发送模板返回 = $this->fs发送课程提醒模板($data['openID'],$m模板信息,$voCenter,$url);
            } elseif ($数据表=='开课通知') {
                $发送模板返回 = $this->fs发送_课程开课通知_模板($data['openID'], $m模板信息, $voCenter, $url);
            } elseif ($数据表=='投票') {
                $发送模板返回 = $this->fs发送审核提醒模板($data['openID'], $m模板信息, $voCenter, $url);
            } else {
                $发送模板返回 = $this->fs发送作业消息模板($data['openID'],$m模板信息,$voCenter,$url);
            }

            if ($发送模板返回->errmsg == 'ok') {
                $this->fs发送信息记录($data['openID'],'作业消息模板','是',$发送模板返回->errcode,$m模板信息['标题'],$data['公众号ID'],$data['公众号openID'],$数据表.$m模板信息['id'],$发送结果['id']);
            } else {
                $this->fs发送信息记录($data['openID'],'作业消息模板','否',$发送模板返回->errcode,$m模板信息['标题'] ,$data['公众号ID'],$data['公众号openID'],$数据表.$m模板信息['id'],$发送结果['id']);
            }

        } else {
            $this->fs发送信息记录($data['openID'],'图文消息','是',$发送图文返回->errcode ,$m模板信息['标题'] ,$data['公众号ID'],$data['公众号openID'],$数据表.$m模板信息['id'],$发送结果['id']);
        }
        return true;
    }


    /**
     * @param $用户oid
     * @return bool|C信息中心
     */
    private function fs实例化信息中心($openid) {
        if (!$openid) $openid = G_DEFAULT_USER_OPENID;
        $voCenter = C信息中心::get实列根据用户Oid($openid);
        return $voCenter;
    }

    public function fs发送审核提醒模板($openID,$模板信息,C信息中心 $voCenter,$url){
        $模板信息['咨询模板ID'] = trim($模板信息['modle_id']);
        $发送模板结果= $voCenter->ff发送模板消息($模板信息['咨询模板ID'],$openID,array(
            'first'=>array('value'=> "开始投票啦，期待您的参与~",'color'=>'#173177'),
            'keyword1'=>array('value'=>'寻找最美儿科医生','color'=>'#173177'),
            'keyword2'=>array('value'=> $模板信息['release_date'],'color'=>'#173177'),
            'remark'=>array('value'=> $模板信息['descript'],'color'=>'#173177')

        ), G_SERV_ID,$url);

        return $发送模板结果;
    }

    public function fs发送作业消息模板($openID,$模板信息,C信息中心 $voCenter,$url){
        $模板信息['咨询模板ID'] = trim($模板信息['咨询模板ID']);
        $发送模板结果= $voCenter->ff发送模板消息($模板信息['咨询模板ID'],$openID,array(
            'first'=>array('value'=> $模板信息['标题'],'color'=>'#173177'),
            'keyword1'=>array('value'=>$模板信息['消息类别'],'color'=>'#173177'),
            'keyword2'=>array('value'=> $模板信息['发布人'],'color'=>'#173177'),
            'keyword3'=>array('value'=> $模板信息['发布时间'],'color'=>'#173177'),
            'remark'=>array('value'=>$模板信息['追加'],'color'=>'#173177')
        ), G_SERV_ID,$url);

        return $发送模板结果;
    }

    public function fs发送课程提醒模板($openID,$模板信息,C信息中心 $voCenter,$url){
        $模板信息['咨询模板ID'] = trim($模板信息['咨询模板ID']);
        $发送模板结果= $voCenter->ff发送模板消息($模板信息['咨询模板ID'],$openID,array(
            'first'=>array('value'=> '开课啦！点击立即报名！','color'=>'#173177'),
            'keyword1'=>array('value'=>$模板信息['title'],'color'=>'#173177'),
            'keyword2'=>array('value'=> $模板信息['name'],'color'=>'#173177'),
            'remark'=>array('value'=>$模板信息['theme'],'color'=>'#173177')
        ),G_SERV_ID,$url);

        return $发送模板结果;
    }

    public function fs发送_课程开课通知_模板($openID,$模板信息,C信息中心 $voCenter,$url) {
        $模板信息['咨询模板ID'] = trim($模板信息['咨询模板ID']);
        $发送模板结果= $voCenter->ff发送模板消息($模板信息['咨询模板ID'],$openID,array(
            'userName'=>array('value'=> $模板信息['userName'],'color'=>'#173177'),
            'courseName'=>array('value'=>$模板信息['courseName'],'color'=>'#173177'),
            'date'=>array('value'=> $模板信息['date'],'color'=>'#173177'),
            'remark'=>array('value'=>$模板信息['remark'],'color'=>'#173177')
        ),G_SERV_ID,$url);

        return $发送模板结果;
    }

    private  function fs发送信息记录($接收消息用户openID,$type,$结果,$错误代码,$content,$公众号ID,$公众号openID,$信息id,$id =null){
        $do = new Do消息发送记录();
        $do->set_id($id);
        $do->set_接收消息用户openID($接收消息用户openID);
        $do->set_消息类型($type);
        $do->set_发送时间(Date('Y-m-d H:i:s'));
        $do->set_是否正确发送($结果);
        $do->set_错误代码($错误代码);
        $do->set_消息内容($content);
        $do->set_公众号ID($公众号ID);
        $do->set_公众号openID($公众号openID);
        $do->set_信息id($信息id);
        $do->save();

        if ($错误代码 == '43004') {
            $doUser = new Do用户();
            $doUser->set_unsubscribe(1);
            $doUser->update(array('openID' => $接收消息用户openID));
            Logger::getInstance()->logInfo("用户已取消关注 openid：".$接收消息用户openID);
        }

    }

    public function get未认证医师列表($act,$当前页,$参数,$关键词){
        $where = null;
        if(!Empty($关键词)) {
            $where ="  and  b.name like '%$关键词%' ";
        }
        if($act == '1'){
            $状态='待审核';
        }else if($act == '2'){
            $状态='未通过';
        }else if($act == '3'){
            $状态='通过';
        }
        $sql = "select a.*, b.`头像`,c.区域 as 区域 from 医师详情 a LEFT JOIN `用户` b ON(b.id = a.用户id) left join 区域 as c on a.区域id = c.id  WHERE   a.状态='{$状态}' $where and b.公众号id ='". G_SERV_ID ."'  order by a.id desc ";

        return pdo()->page($sql,$当前页,10,$参数);
    }
    public function f通过sql分页($sql,$当前页码,$参数){

       return pdo()->page($sql,$当前页码,10,$参数);
    }



    public function ff发送文本消息_多公众号($vs消息文本,$vs用户openId,$vs公众号Id){

        $retinfo = new C返回信息();
        //todo 需要获取正确的公众号ID
        $retinfo->set公众号Id($vs公众号Id);
        $retinfo->set用户OpenId($vs用户openId);
        $retinfo->set消息类型("文本消息");
        $retinfo->set消息内容("$vs消息文本");
        // logger()->logDebug2("发送信息".$retinfo);
        self::$返回堆栈->push_数据($retinfo);

        return true;
    }

    public function ff发送图文消息_多公众号(Cbo图文消息列表 $voMsg,$vs用户openId,$vs公众号Id){
        $retinfo = new C返回信息();
        //todo 此处需要获取正确的公众号ID
        $retinfo->set公众号Id($vs公众号Id);
        $retinfo->set用户OpenId($vs用户openId);
        $retinfo->set消息内容($voMsg);
        $retinfo->set消息类型("图文消息");

        self::$返回堆栈->push_数据($retinfo);
        return true;
    }
    public function Get群列表($当前页,$参数,$关键词){
        $where = null;
        if(!Empty($关键词)) {
            $where ="  where  a.群名称 like '%$关键词%' ";
        }
        $sql = "select a.*,b.name as 管理员  from 群详情 as a left join 用户 as b on a.群主id = b.id  $where order by a.id desc";
        return pdo()->page($sql,$当前页,10,$参数);
    }

    public function Get群成员列表($当前页,$参数,$关键词,$id ){
        $where = null;
        if(!Empty($关键词)) {
            $where ="  and  c.name like '%$关键词%' ";
        }
        $sql = "select a.*,b.群名称 ,c.name from 群成员 as a left join 群详情 as b  on a.群id  = b. id left join 用户 as c on a.用户id = c.id where a.群id =$id  $where  order by a.id desc";
        return pdo()->page($sql,$当前页,10,$参数);
    }
    public function Get群聊天列表($当前页,$参数,$关键词,$id ){
        $where = null;
        if(!Empty($关键词['keywords'])) {
            $where ="  and  a.聊天内容 like '%{$关键词['keywords']}%' ";
        }
        if(!Empty($关键词['name'])) {
            $where .="  and  b.name like '%{$关键词['name']}%' ";
        }
        $sql = "select a.*,b.name from 群聊 as a left join 用户 as b on a.用户id = b.id  where 群id =$id  $where  order by a.id desc";
        return pdo()->page($sql,$当前页,10,$参数);
    }

    public function Get模板消息($当前页,$参数 ){

        $sql = "select * from `咨询信息` ORDER BY id DESC ";

        return pdo()->page($sql,$当前页,10,$参数);
    }

    public function Get个人动态($当前页,$参数,$关键词 ){
        $where = null;
        if(!Empty($关键词)) {
            $where =" AND  a.`内容`  like '%$关键词%' ";
        }
        $sql = "SELECT a.*,b.`name` , c.`医院` FROM `个人动态` a  LEFT JOIN 用户 b ON(a.`用户id` =b.id) LEFT JOIN `医师详情` c  ON a.`用户id` =c.`用户id` WHERE   a.`显示状态`='显示' $where   order by a.id desc";

        return pdo()->page($sql,$当前页,10,$参数);
    }

    function  发送文字通知($infoUser,$openid,$公众号id){
        $voCenter=C信息中心::get实列根据用户Oid($openid);
        $voCenter->ff发送文本消息_多公众号($infoUser,$openid,$公众号id);
        $res = Cx消息路由::ff处理返回堆栈();
    }



    public function ff发送病例回复通知文本消息($openid,$caseId,$病历文章)
    {
        $url = $this->f页面链接('Seller19BingliAS_show', array('id' => $caseId));
        $infoUser = "您参与的病例讨论有新回复啦！<a href='$url'>点此查看</a>";
        $voCenter=C信息中心::get实列根据用户Oid($openid);
        $res = $voCenter->ff发送文本消息($infoUser,$openid);
        $result = json_decode($res);
        if($result->errmsg!="ok"){
            $this->ff病例回复模板消息($voCenter,$openid,$url,$病历文章);
        }
    }
    public function ff发送病例回复通知文本消息_new($openid,$caseId,$病历文章)
    {
        $url = $this->f页面链接_new('case', 'case_detail',array('id' => $caseId));
        $infoUser = "您参与的病例讨论有新回复啦！<a href='$url'>点此查看</a>";
        $voCenter=C信息中心::get实列根据用户Oid($openid);
        $res = $voCenter->ff发送文本消息($infoUser,$openid);
        $result = json_decode($res);
        if($result->errmsg!="ok"){
            $this->ff病例回复模板消息($voCenter,$openid,$url,$病历文章);
        }
    }
    public function ff病例回复模板消息(C信息中心 $voCenter,$openID,$url,$病历文章){
        $info = '您好！您参与的病例讨论有了新回复，点击查看';
        global $G_CONFIG;
        $date = date("Y-m-d H:i:s",time());
        $模板信息 = array(
            '咨询模板ID' => $G_CONFIG['模板消息ID']['问题回复通知'],
            'title' => $info,
            'content' => $病历文章['title'],
            'date' => $date,
        );
        $模板信息['咨询模板ID'] = trim($模板信息['咨询模板ID']);
        $发送模板结果= $voCenter->ff发送模板消息($模板信息['咨询模板ID'],$openID,array(
            'first'=>array('value'=> $模板信息['title'],'color'=>'#173177'),
            'keyword1'=>array('value'=>$模板信息['content'],'color'=>'#173177'),
            'keyword2'=>array('value'=> $模板信息['date'],'color'=>'#173177')
        ), G_SERV_ID,$url);

        return $发送模板结果;
    }

//   public function ff发送医师审核文本消息($infoUser,$user,$status)
//    {
//        $voCenter=C信息中心::get实列根据用户Oid($user['openID']);
//        $res = $voCenter->ff发送文本消息($infoUser,$user['openID']);
//        $msg = '医师审核文本消息';
//        $result = json_decode($res);
//        $send_result = '是';
//        $errcode = $result->errcode;
//        if($result->errmsg!="ok"){
//            $msg = '医师审核模板消息';
//            $res = $this->ff认证模板消息($voCenter,$infoUser,$status,$user['openID']);
//            $errcode = $res->errcode;
//            if($res->errmsg != 'ok'){
//                $send_result = '否';
//            }
//        }
//        if($status == '未通过'){
//            $this->fs发送信息记录($user['openID'],$msg,$send_result,$errcode ,$infoUser ,$user['公众号ID'],$user['公众号openID'],'医师审核未通过');
//        }
//    }
    public function ff认证模板消息(C信息中心 $voCenter,$infoUser,$status,$openID){
        global $G_CONFIG;
        $date = date("Y-m-d H:i:s",time());
        $info = preg_replace('/\\\n/','，',$infoUser);
        $模板信息 = array(
            '咨询模板ID' => $G_CONFIG['模板消息ID']['审核结果通知'],
            'title' => $info,
            'content' => "认证服务",
            'result' => $status,
            'remark' => $date,
        );
        $模板信息['咨询模板ID'] = trim($模板信息['咨询模板ID']);
        $发送模板结果= $voCenter->ff发送模板消息($模板信息['咨询模板ID'],$openID,array(
            'first'=>array('value'=> $模板信息['title'],'color'=>'#173177'),
            'keyword1'=>array('value'=>$模板信息['content'],'color'=>'#173177'),
            'keyword2'=>array('value'=> $模板信息['result'],'color'=>'#173177'),
            'remark'=>array('value'=>$模板信息['remark'],'color'=>'#173177')
        ), G_SERV_ID,'');

        return $发送模板结果;
    }

    public function daochu($tablename, $act="", $time=""){


        require_once 'tools/excle/excel.class.php';
        $xls= new excel('UTF-8',false,$tablename);

        $data[1]=array('姓名','微信名','电话','医院','职务','状态','注册时间');
        $sql = sprintf("SELECT a.name 姓名,a.name_wx 微信名,a.telephone 电话,b.医院,b.简介 AS 职务,b.`状态`,a.regist_time as 注册时间 from 用户 a LEFT JOIN `医师详情` b ON a.id = b.`用户id` WHERE a.公众号ID ='%s'", G_SERV_ID);

        if($act!=""){
            $sql.=" and b.状态 ='".$act."'";

        }
        if($time!=""){

            $sql.=" and a.regist_time > '".$time."'";
        }
        $sql.=' ORDER BY a.regist_time';
        $list=pdo()->selectAll($sql);
        for ($i=0;$i<count($list);$i++){
            $data[]=array("".$list[$i]['姓名']."","".$list[$i]['微信名']."","".$list[$i]['电话']."","".$list[$i]['医院']."","".$list[$i]['职务']."","".$list[$i]['状态']."","".$list[$i]['注册时间']."");
        }
        $xls->addArray($data);
        $xls->generateXML($tablename);
    }
}